<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>登录</title>
  <!-- 导入bootstrap -->
  <script src="./assets/lib/jquery-3.6.0.min.js"></script>
  <link rel="stylesheet" href="./assets/lib/bootstrap/bootstrap.min.css" />
  <script src="./assets/lib/bootstrap/bootstrap.min.js"></script>
  <!-- bootstrap提示框 -->
  <script src="./assets/lib/toasts.js"></script>
  <!-- 导入字体图标 -->
  <link rel="stylesheet" href="./assets/iconfont/iconfont.css" />
  <!-- 导入axios + axios全局配置 -->
  <script src="./assets/lib/axios.js"></script>
  <script src="./assets/utils/request.js"></script>
  <!-- 导入自己写的css -->
  <link rel="stylesheet" href="./assets/css/login.css" />


</head>

<body>
  <div class="area">
    <div class="top">
      <img src="./assets/images/logo2.png" class="col-6" alt="" />
      <h1>学员管理系统</h1>
    </div>
    <div class="row">
      <div class="left">
        <img src="./assets/images/slogan.png" alt="" />
      </div>
      <div class="form-box">
        <div class="register box">
          <div class="container row header mb-4">
            <h2>欢迎注册</h2>
          </div>
          <form action="" class="container rForm">
            <!-- 账号 -->
            <div class="row mb-4 form-group">
              <span class="iconfont icon-yonghu"></span>
              <input type="text" class="form-control" name="username" placeholder="请输入账号" />

            </div>
            <!-- 密码 -->
            <div class="row mb-4 form-group">
              <span class="iconfont icon-mima"></span>
              <input type="password" class="form-control" name="password" placeholder="请输入密码" />
            </div>
            <div class="row mb-4">
              <button type="submit" class="btn btn-primary" id="register">注册</button>
            </div>
            <div class="row mb-3">
              <a href="javascript:;" class="goLogin">已有账号，去登录</a>
            </div>
          </form>
        </div>

        <div class="login box">
          <div class="container row header mb-4">
            <h2>欢迎登录</h2>
          </div>
          <form action="" class="container lForm">
            <!-- 账号 -->
            <div class="row mb-4 form-group">
              <span class="iconfont icon-yonghu"></span>
              <input type="text" class="form-control" name="username" placeholder="请输入账号" />
            </div>
            <!-- 密码 -->
            <div class="row mb-4 form-group">
              <span class="iconfont icon-mima"></span>
              <input type="password" class="form-control" name="password" placeholder="请输入密码" />
            </div>
            <div class="row mb-4">
              <button type="submit" class="btn btn-primary" id="login">登录</button>
            </div>
            <div class="row mb-3">
              <a href="javascript:;" class="goRegister">还没有账号，去注册</a>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

  <!-- 页面js代码 -->
  <script src="./assets/js/login.js"></script>
  <script src="./assets/lib/form-serialize.js"></script>
  <script>
    // (1)收集表单数据
    // 2)校验表单数据（非空+正则）
    // 3)发送ajax请求（根据接口文档）
    // 4)服务器响应（保存token,跳转首页）
    document.querySelector('.goRegister').addEventListener('click', () => {
      document.querySelector('.login').style.display = 'none'
      document.querySelector('.register').style.display = 'block'
    })
    document.querySelector('.goLogin').addEventListener('click', () => {
      document.querySelector('.register').style.display = 'none'
      document.querySelector('.login').style.display = 'block'
    })



    // 登录功能
    const lForm = document.querySelector('.lForm')
    document.querySelector('#login').addEventListener('click', async e => {
      e.preventDefault()
      const data = serialize(lForm, { hash: true, empty: true })
      // console.log(data);
      const { username, password } = data
      if (username.length < 2 || username.length > 30) {
        console.log('用户名为2~30个字符')
        return
      }

      if (password.length < 6 || password.length > 30) {
        console.log('密码为6~30个字符')
        return
      }


      const res = await axios({
        url: 'http://www.itcbc.com:8000/api/login',
        method: 'post',
        data
      })
      console.log(res);
      if (res.data.code === 0) {
        location.href = './index.html'
        const js = JSON.parse(res.config.data)
        console.log(js);
        localStorage.setItem('token', res.data.token)
        localStorage.setItem('username', JSON.stringify(js.username))
      } else {
        alert(res.data.message)
      }
    })

    // 注册功能
    const rForm = document.querySelector('.rForm')
    document.querySelector('#register').addEventListener('click',async e => {
      e.preventDefault()
     const data = serialize(rForm, { hash: true, empty: true })
    //  console.log(data);
     const {username, password} = data
     if (username.length < 2 || username.length > 30) {
        console.log('用户名为2~30个字符')
        return
      }

      if (password.length < 6 || password.length > 30) {
        console.log('密码为6~30个字符')
        return
      }

     const res = await axios({
      url: 'http://www.itcbc.com:8000/api/register',
      method: 'post',
      data
      })
      console.log(res);
      if (res.data.code === 0) {
        document.querySelector('.goLogin').click()
      } else {
        alert(res.data.message)
      }
    })
  </script>

</body>

</html>